<template>
    <div class="pane-wrap">
        <div class="rs-toolbar">
            <div class="title">当前操作的机构：{{unitName}}</div>
            <el-button-group>
                <el-button type="warning" size="small" icon="rs-icon-user-add-fill" @click="onAddUser" :disabled="btnDisabled">添加人员</el-button>
                <!-- <el-button type="danger" size="small" icon="rs-icon-user-add-fill" @click="onDelUser" :disabled="btnDelDisabled">删除人员</el-button> -->
            </el-button-group>
            <el-input v-model="yhmc" type="text" clearable placeholder="请输入用户名..." size="small" style="width:200px;vertical-align: middle;"></el-input>
            <el-button-group>
                <el-button type="success" size="small" icon="el-icon-search" @click="loadUsers">查询</el-button>
                <el-button type="danger" size="small" icon="el-icon-circle-close" @click="onClearQuery">清空</el-button>
            </el-button-group>
        </div>
        <el-table
            ref="userTable"
            @selection-change="onSelectionChange"
            :height="tableHeight"
            :data="users"
            border
            style="width: 100%"
            size="small">
                <el-table-column type="selection" label="" width="50" align="center"></el-table-column>
                <el-table-column prop="yhmc" label="用户名称" width="180"></el-table-column>
                <el-table-column prop="dlmc" label="登录名称"></el-table-column>
        </el-table>
        <RsPagination :currentPage="currentPage" :pageSize="pageSize" :totalCount="totalCount" @change="onPageInfoChange"></RsPagination>
        <UserForm ref="form" :depts="depts"></UserForm>
        <UserAddDlg ref="userAddDlg"></UserAddDlg>
    </div>
</template>
<script>
import tips from '../../mixin/tips'
import mixin from './mixin';
import UserForm from '../user/userForm';
import UserAddDlg from './userAddDlg'
import deepmerge from 'deepmerge';
import unit from '@/api/unit';
//import UserMethods from '../mixin/UserMethod';
export default {
    mixins:[mixin,tips],
    inject: ['unit'],
    components:{UserForm,UserAddDlg},
    data(){
        return {
            yhmc:'',
            totalCount:0,
            pageSize:20,
            currentPage:0,
            users: [],
            updateList:false,
            selectedRows:[]
        }
    },
    watch:{
        currentNode(newValue,oldValue){

            if( this.unit.activeName ==='user'){
                this.currentPage = 0;
                this.totalCount = 0;
                const deptData = newValue.data;
                this.loadUsers();
            }else{
                this.updateList = true;
                this.users = [];
            }
        },
        'unit.activeName'(newValue,oldValue){

            if( this.unit.activeName ==='user' && this.updateList ){
                this.currentPage = 0;
                this.totalCount = 0;
                const deptData = this.currentNode.data;
                this.loadUsers();
                this.updateList = false;
            }
        }
    },
    computed:{
        queryData(){
            const deptData = this.currentNode.data;
            return {
                bmbh:deptData.bmbh,
                yhmc:this.yhmc,
                page:this.currentPage,
                pageSize:this.pageSize
            }
        },
        depts(){
            return this.unit.depts;
        },
        btnDisabled(){
            return this.currentNode == null || this.currentNode.data.bmbh ==='FFFFFFFFF'
        },
        btnDelDisabled(){

            return this.selectedRows.length == 0 || this.currentNode == null || this.currentNode.data.bmbh ==='FFFFFFFFF'
        }
    },
    methods:{
        onPageInfoChange(obj){
            this.pageSize = obj.pageSize;
            this.currentPage = obj.currentPage;
            this.loadUsers();
        },
        onClearQuery(){
            this.yhmc = '';
        },
        onAddUser(){
            const unitData = this.currentNode.data;
            this.$refs.userAddDlg.show(unitData)
            .then((action)=>{
                if(action ==='ok'){
                    this.loadUsers();
                }
            });
        },
        onDelUser(){

            if( this.selectedRows.length == 0)
                return;
            this.$confirm('是否要删除？', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
                closeOnClickModal:false
            }).then((action) => {
                this.delUsers();
            }).catch((action) => {

            });

        },
        delUsers(){

            const postData = {
                bmbh:this.currentNode.data.bmbh,
                ids:this.selectedRows.map((item)=>item.yhbh)
            }
            unit.delUser(postData)
            .then(response => {
                const { data } = response;
                if( data.error_code == 0 ){
                    this.loadUsers();
                }else{
                    this.showError("操作失败！");
                }
            }).catch(error => {
                this.showError("本地异常！");
            });
        },
        onSelectionChange(rows){
            this.selectedRows = rows;
        },
        loadUsers(){
            //console.log(this.queryData)
            this.loading = true;
            unit.queryUser(this.queryData)
            .then(response => {
                this.loading = false;
                const { data } = response;
                if( data.error_code == 0 ){
                    
                    this.users = data.list;
                    this.totalCount = data.totalCount;
                }else{
                    this.showError("操作失败！");
                }
            }).catch(error => {
                this.loading = false;
            });
        }
    }
}
</script>